package cn.wolfcode.trip.app.util;
import cn.wolfcode.trip.base.domain.OAuthInfo;
import cn.wolfcode.trip.base.domain.Param;

public class WeiXinUtil {
    private static  String GET_USER_INF_URL= Param.USER_INFO_URL;
    /**
     * 第一步：用户同意授权，获取code(引导关注者打开如下页面：)
     *  获取 code、state
     */
    public static String getStartURLToGetCode(String uuid) {
        /*String takenUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
        takenUrl= takenUrl.replace("APPID", "wxab223347608a2a0a");
        takenUrl= takenUrl.replace("REDIRECT_URI", "https%3A%2F%2F9e910cff.ngrok.io%2FloginByWeiXin%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60");
        //FIXME ： snsapi_userinfo
        takenUrl= takenUrl.replace("SCOPE", "snsapi_userinfo");*/

        String takenUrl = Param.CODE_URL;
        takenUrl= takenUrl.replace("APPID", Param.APPID);
        takenUrl= takenUrl.replace("REDIRECT_URI", Param.REDIRECT_URI);
        //FIXME ： snsapi_userinfo
        takenUrl= takenUrl.replace("SCOPE", Param.SCOPE);
        takenUrl= takenUrl.replace("STATE", uuid);
        System.out.println(takenUrl);
        return takenUrl;
    }

    /**
     * 获取access_token、openid
     * 第二步：通过code获取access_token
     * @param code url = "https://api.weixin.qq.com/sns/oauth2/access_token
     *                      ?appid=APPID
     *                      &secret=SECRET
     *                      &code=CODE
     *                      &grant_type=authorization_code"
     * */
    public static OAuthInfo getAccess_token(String code){
        /*String authUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        authUrl= authUrl.replace("APPID", "wxab223347608a2a0a");
        authUrl = authUrl.replace("SECRET","159be553fb02ed484abf0898556161b1");
        authUrl = authUrl.replace("CODE", code);
        String jsonString = HttpClientUtil.doGet(authUrl);
        System.out.println("jsonString: " + jsonString);*/
        String authUrl = Param.ACCESS_TOKEN_URL;
        authUrl= authUrl.replace("APPID", Param.APPID);
        authUrl = authUrl.replace("SECRET",Param.SECRET);
        authUrl = authUrl.replace("CODE", code);
        String jsonString = HttpClientUtil.doGet(authUrl);
        System.out.println("jsonString: " + jsonString);
        OAuthInfo auth = null;
        try {
            auth = JsonUtils.jsonToPojo(jsonString, OAuthInfo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return auth;
    }


    /**
     * 获取微信用户信息
     * @param oAuthInfo
     * @return 返回JSON格式的用户信息
     */
    public static String getUserInfo(OAuthInfo oAuthInfo){
        GET_USER_INF_URL= GET_USER_INF_URL.replace("ACCESS_TOKEN", oAuthInfo.getAccess_token());
        GET_USER_INF_URL= GET_USER_INF_URL.replace("OPENID", oAuthInfo.getOpenid());
        String userInfoStr = HttpClientUtil.doGet(GET_USER_INF_URL);
        return userInfoStr;
    }


    public static String getStartURLToGetCodeForBind(String userIdStr) {
        String takenUrl = Param.CODE_URL;
        takenUrl= takenUrl.replace("APPID", Param.APPID);
        takenUrl= takenUrl.replace("REDIRECT_URI", Param.REDIRECT_URI_FOR_BIND);
        //FIXME ： snsapi_userinfo
        takenUrl= takenUrl.replace("SCOPE", Param.SCOPE);
        takenUrl= takenUrl.replace("STATE", userIdStr);
        return takenUrl;
    }
}
